<?php

//Valid dd-mm-yyyy
function isValidDate($date)
{
	if(preg_match("/^(\d{2})-(\d{2})-(\d{4})$/", $date, $matches))
	{
		if(checkdate($matches[2], $matches[1], $matches[3]))
		{
			return true;
		}
	}
}


function isValidDBDate($date)
{
	if(preg_match("/^(\d{4})-(\d{2})-(\d{2})$/", $date, $matches))
	{
		if(checkdate($matches[2], $matches[3], $matches[1]))
		{
			return true;
		}
	}
}

function toReadableDate($date, $full=true)
{
	if(preg_match("/^(\d{2})-(\d{2})-(\d{4})$/", $date, $matches))
	{
		if(checkdate($matches[2], $matches[1], $matches[3]))
		{
			$monthNames = array("Ene","Feb","Mar","Abr","May","Jun","Jul","Ago","Sep","Oct","Nov","Dic");
		
			$date = $matches[1]." ".$monthNames[$matches[2]-1];
			if($full) $date .=" ".$matches[3];
		}
	}
	
	return $date;

}



function getSeparator($input)
{
    $separator_type= array(
        "/",
        "-",
        "."
    );
	$separator_used="";
    foreach ($separator_type as $separator) {
        $find= stripos($input,$separator);
        if($find<>false){
            $separator_used= $separator;
        }
    }
	return $separator_used;
}

function datecheck($input,$separator,$format="dmy")
{
   
	if($separator != ""){
        $input_array= explode($separator,$input);
        if ($format=="mdy") {
            return checkdate($input_array[0],$input_array[1],$input_array[2]);
        } elseif ($format=="ymd") {
            return checkdate($input_array[1],$input_array[2],$input_array[0]);
        } else {
            return checkdate($input_array[1],$input_array[0],$input_array[2]);
        }
        $input_array=array();
	}	
	return false;
}

function convertDateToDbFormat($date, $separator="-")
{
	if(!empty($separator) && !empty($date)){
		list($day, $month, $year) = explode($separator, $date);
		return date("Y-m-d",mktime(0, 0, 0, $month, $day, $year));
	}else{
		return "";
	}
}

function convertDateToUserFormat($date, $separator="-")
{
	if(!empty($separator) && !empty($date)){
		list($year, $month, $day) = explode($separator, $date);
		return date('d'.$separator.'m'.$separator.'Y', mktime(0, 0, 0, $month, $day, $year));
	}else{
		return "";
	}
}

?>